package com.wgts.wgts.control.oss;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;

import java.text.SimpleDateFormat;
import java.util.Date;

import static okhttp3.internal.Internal.instance;

/*
 *
 *
 *   data:2018/10/169:37
 *
 */
public class MyOSSUtils {

    private static MyOSSUtils instance;

    private final String P_ENDPOINT ="oss-cn-beijing.aliyuncs.com";//主机地址（OSS文档中有提到）

    private final String ACCESS_KEY_ID = "LTAIbsyxtT6VUYr3";

    private final String ACCESS_KEY_SECRET = "yCHqE7DOWltNIxSWDBrH9X6cZdGSBN";

    private final String P_BUCKETNAME ="mohu-product";//（文件夹名字）

    private OSS oss;

    private SimpleDateFormat simpleDateFormat;

    public MyOSSUtils() {

    }

    public static MyOSSUtils getInstance() {

        if (instance ==null) {

            if (instance ==null) {

                return new MyOSSUtils();

            }

        }

        return instance;

    }

    private void getOSs(Context context) {
        //OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");
        //这个初始化安全性没有Sts安全，如需要很高安全性建议用OSSStsTokenCredentialProvider创建（上一行创建方式）多出的参数SecurityToken为临时授权参数
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(8); // 最大并发请求数，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次

        // oss为全局变量，endpoint是一个OSS区域地址
        oss = new OSSClient(context, P_ENDPOINT, credentialProvider, conf);

        if (simpleDateFormat==null){

            simpleDateFormat =new SimpleDateFormat("yyyyMMdd");

        }

    }

    /**

     * 上传图片 上传文件

     *

     * @param context application上下文对象

     * @param ossUpCallback 成功的回调

     * @param img_name 上传到oss后的文件名称，图片要记得带后缀 如：.jpg

     * @param imgPath 图片的本地路径

     */

    public void upImage(Context context,final MyOSSUtils.OssUpCallback ossUpCallback,final String img_name, String imgPath) {

        getOSs(context);

        final Date data =new Date();

        data.setTime(System.currentTimeMillis());

        PutObjectRequest putObjectRequest =new PutObjectRequest(P_BUCKETNAME,simpleDateFormat.format(data)+"/"+  img_name, imgPath);

        putObjectRequest.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request,long currentSize,long totalSize) {

                ossUpCallback.inProgress(currentSize, totalSize);

            }

        });

        oss.asyncPutObject(putObjectRequest,new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {

                Log.e("MyOSSUtils","------getRequestId:" + result.getRequestId());

// try {

                ossUpCallback.successImg(oss.presignPublicObjectURL(P_BUCKETNAME,simpleDateFormat.format(data)+"/"+img_name));

// } catch (ClientException e) {

// e.printStackTrace();

// }

            }

            @Override

            public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {

                ossUpCallback.successImg(null);

            }

        });

    }

    /**

     * 上传图片 上传流

     *

     * @param context application上下文对象

     * @param ossUpCallback 成功的回调

     * @param img_name 上传到oss后的文件名称，图片要记得带后缀 如：.jpg

     * @param imgbyte 图片的byte数组

     */

    public void upImage(Context context,final MyOSSUtils.OssUpCallback ossUpCallback,final String img_name,byte[] imgbyte) {

        getOSs(context);

        final Date data =new Date();

        data.setTime(System.currentTimeMillis());

        PutObjectRequest putObjectRequest =new PutObjectRequest(P_BUCKETNAME,simpleDateFormat.format(data)+"/"+ img_name, imgbyte);

        putObjectRequest.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {

            @Override

            public void onProgress(PutObjectRequest request,long currentSize,long totalSize) {

                ossUpCallback.inProgress(currentSize, totalSize);

            }

        });

        oss.asyncPutObject(putObjectRequest,new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {

            @Override

            public void onSuccess(PutObjectRequest request, PutObjectResult result) {

                Log.e("MyOSSUtils","------getRequestId:" + result.getRequestId());

                ossUpCallback.successImg(oss.presignPublicObjectURL(P_BUCKETNAME,simpleDateFormat.format(data)+"/"+img_name));

            }

            @Override

            public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {

                ossUpCallback.successImg(null);

            }

        });

    }

    /**

     * 上传视频

     *

     * @param context application上下文对象

     * @param ossUpCallback 成功的回调

     * @param video_name 上传到oss后的文件名称，视频要记得带后缀 如：.mp4

     * @param video_path 视频的本地路径

     */

    public void upVideo(Context context,final MyOSSUtils.OssUpCallback ossUpCallback,final String video_name, String video_path) {

        getOSs(context);

        final Date data =new Date();

        data.setTime(System.currentTimeMillis());

        PutObjectRequest putObjectRequest =new PutObjectRequest(P_BUCKETNAME,simpleDateFormat.format(data)+"/"+ video_name, video_path);

        putObjectRequest.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {

            @Override

            public void onProgress(PutObjectRequest request,long currentSize,long totalSize) {

                ossUpCallback.inProgress(currentSize, totalSize);

            }

        });

        oss.asyncPutObject(putObjectRequest,new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {

            @Override

            public void onSuccess(PutObjectRequest request, PutObjectResult result) {

                ossUpCallback.successVideo(oss.presignPublicObjectURL(P_BUCKETNAME,simpleDateFormat.format(data)+"/"+video_name));

            }

            @Override

            public void onFailure(PutObjectRequest request, ClientException clientException, ServiceException serviceException) {

                ossUpCallback.successVideo(null);

            }

        });

    }

    public interface OssUpCallback {

        void successImg(String img_url);

        void successVideo(String video_url);

        void inProgress(long progress,long zong);

    }

}
